package com.swhy

import java.net.URI
import java.util.{Date, UUID}

import com.utils._
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.{SparkConf, SparkContext}
import org.codehaus.jackson.map.ObjectMapper


object ChgeLblDictionary {
  def main(args: Array[String]): Unit = {
    if (args.length != 2) {
      println(
        """
          |com.swhy.CompanyInfo
          |params:
          | InputDate    刷数日期
          | OutputPath   输出路径
        """.stripMargin)
      sys.exit()
    }
    val conf = new Configuration()
    val Array(inputDt, outputPath) = args
    val sparkConf = new SparkConf().setAppName(this.getClass.getName)
    val sc = new SparkContext(sparkConf)
    val hiveContext = new HiveContext(sc)
//改下方的sql为需要装换成jason的sql
    val allcompany =s"""
        |select
        |lbl_type_no,
        |lbl_name,
        |lbl_busi_type,
        |case when lbl_busi_type='sj' then '商机' else '风险' end as lbl_busi_name,
        |'1' as is_pub,
        |null as lbl_config,
        |lbl_type_no as indx_no,
        |lbl_name as indx_name,
        |'SJJG' as create_user,
        |null as lbl_desc,
        |now() as create_time,
        |now() as update_time
        |from
        |dim_data.dim_dao_custchg_dic
      """.stripMargin
    print(allcompany)
    val frame = hiveContext.sql(allcompany)
    val result = frame.rdd.map(x => {
      var cominfo = new StringBuilder()

      val lbl_type_no = x.getAs[String]("lbl_type_no")
      val lbl_name = x.getAs[String]("lbl_name")
      val lbl_busi_type = x.getAs[String]("lbl_busi_type")
      val is_pub = x.getAs[String]("is_pub")
      val lbl_busi_name = x.getAs[String]("lbl_busi_name")
      val lbl_config = x.getAs[String]("lbl_config")
      val indx_no = x.getAs[String]("indx_no")
      val indx_name = x.getAs[String]("indx_name")
      val create_user = x.getAs[String]("create_user")
      val lbl_desc = x.getAs[String]("lbl_desc")
      val create_time = x.getAs[Date]("create_time")
      val update_time = x.getAs[Date]("update_time")


      val _id = new ID()
      val index = new Index()
      val companyInfo = new ChgeLblDictionary()

      _id.set_id(UUID.randomUUID().toString)
      index.setIndex(_id)
      companyInfo.setLblTypeNo(lbl_type_no)
      companyInfo.setLblName(lbl_name)
      companyInfo.setLblBusiType(lbl_busi_type)
      companyInfo.setIsPub(is_pub)
      companyInfo.setLblBusiName(lbl_busi_name)
      companyInfo.setLblConfig(lbl_config)
      companyInfo.setIndxNo(indx_no)
      companyInfo.setIndxName(indx_name)
      companyInfo.setCreateUser(create_user)
      companyInfo.setLblDesc(lbl_desc)
      companyInfo.setCreateTime(create_time)
      companyInfo.setUpdateTime(update_time)

      val mapper = new ObjectMapper()

      val indexJson = mapper.writeValueAsString(index) //企业自增编码json串
      val companyInfoJson = mapper.writeValueAsString(companyInfo) //企业信息串

      cominfo = cominfo.append(indexJson + "\n" + companyInfoJson) //json结果串
      cominfo
    })
    val path = new Path(outputPath) //创建文本输出路径
    val fs = FileSystem.get(URI.create(outputPath), conf)
    if (fs.exists(path)) {
      fs.delete(path, true)
    }
    result.saveAsTextFile(outputPath)
    sc.stop()
  }
}
